import Vue from "vue";
import axios from "axios";
import router from "../../router";
import { message } from "./message";
let common = {
  /**
   * @method get - get请求.
   * @param {string} url - 请求路径.
   */
  get(url, data) {
    return new Promise((resolve, reject) => {
      axios
        .get(url, { params: data, retry: 2, retryDelay: 100 })
        .then((res) => {
          if (res.data.code === 0) {
            resolve(res.data);
          } else {
            sucErrFun(res);
            reject(res.data);
          }
        })
        .catch((err) => {
          errorFun(err);
          reject(err.data);
        });
    });
  },
  /**
   * @method post - post请求.
   * @param {string} url - 请求路径.
   * @param {Object} data - 请求参数.
   */
  post(url, data) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, data, { retry: 2, retryDelay: 100 })
        .then((res) => {
          if (res.data.error_code === 0) {
            resolve(res.data);
          } else {
            sucErrFun(res);
            reject(res.data);
          }
        })
        .catch((err) => {
          console.log(err);
          errorFun(err);
          reject(err.data);
        });
    });
  },
  /**
   * @method download - 下载.
   * @param {string} url - 请求路径.
   * @param {Object} data - 请求参数.
   */
  download(data, name) {
    return new Promise((resolve, reject) => {
      axios({
        method: "post",
        url: "/api/v1/model/file/",
        data: data,
        responseType: "blob",
      })
        .then((res) => {
          let blob = new Blob([res.data], {
            type: "application/zip",
          });
          if (window.navigator.msSaveOrOpenBlob) {
          }
          var link = document.createElement("a");
          link.setAttribute("download", `${name}.zip`);
          link.href = window.URL.createObjectURL(blob);
          link.click();
          window.URL.revokeObjectURL(link.href);
          resolve(res);
        })
        .catch((err) => {
          errorFun(err);
          reject(err.data);
        });
    });
  },
  /**
   * @method download - 文件下载.
   * @param {string} url - 请求路径.
   * @param {Object} data - 请求参数.
   */
  downloadGet(url, data, type, fileName) {
    return new Promise((resolve, reject) => {
      axios({
        method: "get",
        url: url,
        data: data,
        responseType: "blob",
      })
        .then((res) => {
          let blob = new Blob([res.data], {
            type: res.headers[`${type}`],
          });
          if (window.navigator.msSaveOrOpenBlob) {
          }
          var link = document.createElement("a");
          link.setAttribute("download", fileName);
          link.href = window.URL.createObjectURL(blob);
          link.click();
          window.URL.revokeObjectURL(link.href);
          resolve(res);
        })
        .catch((err) => {
          errorFun(err);
          reject(err.data);
        });
    });
  },
  /**
   * @method patch - patch请求.
   * @param {string} url - 请求路径.
   * @param {Object} data - 请求参数.
   */
  patch(url, data) {
    return new Promise((resolve, reject) => {
      axios
        .patch(url, data, { retry: 2, retryDelay: 100 })
        .then((res) => {
          if (res.data.code === 0) {
            resolve(res.data);
          } else {
            sucErrFun(res);
            reject(res.data);
          }
        })
        .catch((err) => {
          errorFun(err);
          reject(err.data);
        });
    });
  },
  /**
   * @method delete - delete请求.
   * @param {string} url - 请求路径.
   * @param {Object} data - 请求参数.
   */
  patch(url, data) {
    return new Promise((resolve, reject) => {
      axios
        .patch(url, data, { retry: 2, retryDelay: 100 })
        .then((res) => {
          if (res.data.code === 0) {
            resolve(res.data);
          } else {
            sucErrFun(res);
            reject(res.data);
          }
        })
        .catch((err) => {
          errorFun(err);
          reject(err.data);
        });
    });
  },
  /**
   * @method put - put请求.
   * @param {string} url - 请求路径.
   */
  put(url, data) {
    return new Promise((resolve, reject) => {
      axios
        .put(url, data, { retry: 2, retryDelay: 100 })
        .then((res) => {
          if (res.data.code === 0) {
            resolve(res.data);
          } else {
            sucErrFun(res);
            reject(res.data);
          }
        })
        .catch((err) => {
          errorFun(err);
          reject(err.data);
        });
    });
  },
  /**
   * @method delete - delete请求.
   * @param {string} url - 请求路径.
   */
  delete(url, data) {
    return new Promise((resolve, reject) => {
      axios
        .delete(url, { data, retry: 2, retryDelay: 100 })
        .then((res) => {
          if (res.data.code === 0) {
            resolve(res.data);
          } else {
            sucErrFun(res);
            reject(res.data);
          }
        })
        .catch((err) => {
          errorFun(err);
          reject(err.data);
        });
    });
  },
  requestAll(arr) {
    return new Promise((resolve, reject) => {
      axios
        .all(arr)
        .then(
          axios.spread(() => {
            resolve("success");
          })
        )
        .catch((err) => {
          reject(err.data);
        });
    });
  },
};
function sucErrFun(res) {
  if (res.data.code === 10060) {
    if (
      res.data.data[Object.keys(res.data.data)[0]] &&
      res.data.data[Object.keys(res.data.data)[0]][0]
    ) {
      let msg = res.data.data[Object.keys(res.data.data)[0]];
      message.warning(msg[0]);
    } else {
      message.warning(res.data.msg);
    }
  } else {
    message.warning(res.data.msg);
  }
}
function errorFun(err) {
  // 没有登录或者token验证失败
  if (err.response) {
    if (err.response.status == 401) {
      localStorage.clear();
      router.replace({ path: "/" }).catch((data) => {});
    }
  }
}
export default common;
